'use strict';
const { Service } = require('egg');
const tencentcloud = require("tencentcloud-sdk-nodejs")

const CosConfig = {
  SecretId:'AKID2zqDdSJG7rUzSjuxpYAh1CAztZ6RdfYE',
  SecretKey:'d9VfcNxJRn391f4f7NSK4gAYkkaCbnmY',
  Bucket: 'diandian-1300713889',
  Region: 'ap-shanghai',
  Uin: '',
};
// 导入对应产品模块的client models。
const TiiaClient = tencentcloud.tiia.v20190529.Client
const { v4: uuidv4 } = require('uuid');
// 实例化要请求产品(以cvm为例)的client对象
const client = new TiiaClient({
  // 为了保护密钥安全，建议将密钥设置在环境变量中或者配置文件中，请参考本文凭证管理章节。
  // 硬编码密钥到代码中有可能随代码泄露而暴露，有安全隐患，并不推荐。
  credential: {
    secretId: CosConfig.SecretId,
    secretKey: CosConfig.SecretKey
  },
  // 产品地域
  region: CosConfig.Region,
  // 可选配置实例
  profile: {
    signMethod: "TC3-HMAC-SHA256", // 签名方法
  },
})

class TiiaService extends Service {
  async createImage(params) {
    console.log('创建参数', params)
    const res = await client.CreateImage({
      // Action: 'CreateImage',
      // Version: '2019-05-29',
      // Region: CosConfig.Region,
      GroupId: 'test',
      EntityId: params.entityId.toString(),
      PicName: params.fileName,
      ImageUrl: params.url
    })
    console.log('创建成功', res)
  }
  async deleteImage(params) {
      await client.DeleteImages({
          // Action: 'DeleteImages',
          // Version: '2019-05-29',
          // Region: CosConfig.Region,
          GroupId: 'test',
          EntityId: params.entityId.toString(),
          PicName: params.fileName
      })
  }
  async searchImage(params) {
    console.log('params',{
      GroupId: 'test',
      ImageUrl: params.url,
      MatchThreshold: 99,
      EnableDetect: true
    })
    return await client.SearchImage({
      // Action: 'SearchImage',
      // Version: '2019-05-29',
      // Region: CosConfig.Region,
      GroupId: 'test',
      ImageUrl: params.url,
      MatchThreshold: 15,
      EnableDetect:true
    })
  }
}

module.exports = TiiaService;
